﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data.OleDb

Public Class ProgressHistory
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Request.Params("UID") = "" Or IsNothing(Request.Params("UID")) Then Return
        Dim query As String = String.Format("select top 9 weight, entrydate from tblProgress where userid=@f1 order by id desc", "afsds")
        Dim dt As DataTable = GetData(query)
        Dim x As String() = New String(dt.Rows.Count - 1) {}
        Dim y As Decimal() = New Decimal(dt.Rows.Count - 1) {}
        For i As Integer = 0 To dt.Rows.Count - 1
            x(i) = dt.Rows(i)(1).ToString().Substring(0, 10)
            y(i) = dt.Rows(i)(0).ToString()
        Next
        LineChart1.Series.Add(New AjaxControlToolkit.LineChartSeries() With { _
         .Name = "Change in Weight (Kgs)!", _
         .Data = y _
        })
        LineChart1.CategoriesAxis = String.Join(",", x)
        LineChart1.ChartTitle = String.Format("Weight Loss History by Date")

        If x.Length > 3 Then
            LineChart1.ChartWidth = (x.Length * 75).ToString()
        End If
        LineChart1.Visible = True

    End Sub

    Private Function GetData(ByVal query As String) As DataTable
        Dim dt As New System.Data.DataTable
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(query, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("UID"))
        oleDbConn.Open()
        Dim newda As New OleDbDataAdapter(cmd)
        Using oleDbConn
            Using cmd
                Using sda As New OleDbDataAdapter(cmd)
                    cmd.CommandType = CommandType.Text
                    cmd.Connection = oleDbConn
                    sda.SelectCommand = cmd
                    sda.Fill(dt)
                End Using
            End Using
            Return dt
        End Using
    End Function

End Class